//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)

let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: {
    view: [
      {
        name: "恢复",
        icon: 'md-create',
        value: 'Edit',
        class: '',
        type: 'success',
        index: 2,//显示的位置
        onClick: function () {
          let params = {
            mainData: {
              memberid: this.getSelectRows()[0].memberid,
              member_black: 0,
            },
            detailData: null
          }
          this.http.post('/api/MebAccount/UpdateBlack', params).then(res => {
            if (res.status) {
              this.$message.success('操作成功')
              this.refresh()
            } else {
              this.$message.error(res.message)
            }
          })
        }
      }
    ], box: [], detail: []
  },//扩展的按钮
  data() {
    return {
      status: true,
      memberid: '',
      cityAreas: []
    }
  },
  methods: {//事件扩展
    getFormOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    rowClick({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    onInit() {
      this.$store.getters.data().viewPageTest = this;
      this.http.post('/api/Dictionary/GetVueDictionary', ['areas']).then(res => {
        this.cityAreas = res[0].data
      })
      let idcardtype = this.getFormOption('idcardtype')
      let idnumber = this.getFormOption('idnumber')
      this.editFormOptions[0][1] = {
        render: (h, { }) => {
          return h(
            "button",
            {
              class: "ivu-btn",
              style: {
                "margin-left": "10px",
              },
              on: {
                click: () => {
                  this.$store.getters.data().viewPageTest.refresh()
                  console.log(this);
                  let param = {
                    order: "desc",
                    page: 1,
                    rows: 30,
                    sort: "",
                    wheres: `[{\"name\":\"member_phone\",\"value\":\"${this.editFormFields.member_phone}\",\"displayType\":\"text\"}]`,
                  };
                  this.http
                    .post("/api/MebAccount/getPageData", param)
                    .then((res) => {
                      if (res.rows.length != 0) {
                        this.memberid = res.rows[0].memberid
                        this.editFormFields.member_name = res.rows[0].member_name
                        this.editFormFields.member_nickname = res.rows[0].member_nickname
                        idcardtype.hidden = true
                        idnumber.hidden = true
                        this.status = true
                      } else {
                        this.editFormFields.member_name = ''
                        this.editFormFields.member_nickname = ''
                        idcardtype.hidden = false
                        idnumber.hidden = false
                        this.status = false
                      }
                    });
                },
              },
            },
            "查询"
          );
        },
      }

    },
    searchBefore(param) {
      let wheres = [{
        'name': 'member_black',
        'value': '1',
        'displayType': 'text'
      }]
      param.wheres.push(...wheres);
      return true;
    },
    searchAfter(e) {
      e.forEach(i => {
        let reg = /.+?(省|市|自治区|自治州|县|区)/g
        if (i.idnumber) {
          let code = i.idnumber.slice(0, 6)
          let name = ''
          this.cityAreas.forEach(j => {
            if (code == j.key) {
              name = j.value
              i.countriesName = name.match(reg)[0]
              i.provinceName = name.match(reg)[1]
              i.cityName = name.match(reg)[2]
            }
          })
        }
      })
      return true;
    },
    addBefore(e) {
      let params = {
        mainData: {
          member_black: 1,
          ...e.mainData
        },
        detailData: []
      }
      if (this.status) {
        params.mainData.memberid = this.memberid
        console.log(params.mainData,"params.mainData")
        params.detailData = null
        delete params.mainData.idcardtype
        delete params.mainData.idnumber
        this.http.post('/api/MebAccount/UpdateBlack', params).then(res => {
          if (res.status) {
            this.boxModel = false;
            this.refresh()
          } else {
            this.$message.error(res.message)
          }
        })
      } else {
        this.http.post('/api/MebAccount/Add', params).then(res => {
          if (res.status) {
            this.boxModel = false;
            this.refresh()
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
  }
};
export default extension;